/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.element;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.enums.ElementType;
import pa165.dominating.species.tos.ElementTo;


import pa165.dominatingspecies.frontend.pages.BasePage;
import pa165.dominatingspecies.service.impl.ElementServiceImpl;

/**
 *
 * @author Ondrej Prazak
 */
@PreAuthorize("hasRole('ADMIN')")
public class CreateElementPage extends BasePage {

    @SpringBean(name = "elementService")
    private ElementServiceImpl elementService;
    private String description;
    private ElementType selected = ElementType.BUSH;

    public CreateElementPage(ElementTo oldOne) {
        Form<CreateElementPage> createForm = new Form<>("createElementForm",
                new CompoundPropertyModel<>(this));
        add(createForm);
        createForm.add(new Label("desc", new StringResourceModel("desc", this, null)));
        createForm.add(new TextArea<String>("description"));
        createForm.add(new Label("eleType", new StringResourceModel("chooseType", this, null)));

        createForm.add(new EnumDropDown<>("types", new PropertyModel<ElementType>(this, "selected")));
        if(oldOne != null){
          selected = oldOne.getType();
          description = oldOne.getDescription();
        }

        Button submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {
                ElementTo ele = new ElementTo();
                ele.setDescription(description);
                ele.setType(selected);
                if (selected == null) {
                    getSession().warn(new StringResourceModel("eleNotSelected", this, null).getString());
                    setResponsePage(new CreateElementPage(ele));
                } else {
                    
                    boolean thrown = false;
                    try {
                        elementService.createElement(ele);
                    } catch (DataAccessException e) {
                        getSession().warn(new StringResourceModel("eleExists", this, null).getString());
                        System.out.println(e.getMessage());
                        setResponsePage(new CreateElementPage(ele));
                        thrown = true;
                    }
                    if (!thrown) {
                        getSession().info(new StringResourceModel("eleSucc", this, null).getString());
                        setResponsePage(ElementsPage.class);
                    }
                }
            }
        };
        createForm.add(submitButton);

        Link<BasePage> goBack = new Link<BasePage>("goBack") {
            @Override
            public void onClick() {
                setResponsePage(new ElementsPage());
            }
        };
        createForm.add(goBack);
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
